Skip to content

[experiment] first draft of init array syntax #143553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dingxiangfei2009
Copy link
Contributor

@dingxiangfei2009 dingxiangfei2009 commented Jul 7, 2025

This patch allows parsing of the syntax init [ v1, v2, v3, .. ] and certain level of type checking on HIR level.

See Draft RFC, under tracking rust-lang/lang-team#336

Dragons ahead

This is a draft PR that is very experimental and it is posted only for very early feedback and it is not immediately mergeable.

  • This patch introduces changes to the parser to accept init # ... AST fragment as a start.
    The reason that this deviates from the original proposal, with the additional #, is to help with syntax ambiguity between init[0] as indexing into a value init, versus init [ 0 ] as in-place initialisation.
    However, I would propose to switch to a macro based approach to the syntax to avoid such ambiguity.
  • This patch introduces a new type category Init. It has the same generics structure as a Closure and it is almost type-checked in the same way as Closure, except that its return type expectation also contains the error type, which is to be unified with those from the nested in-place initialisation.
    This is still not functional, because for instance I still have to fill out some tables such as late_bound_vars.

This patch does not include any THIR or MIR building.

The final state at which I would see this patch can be merged, is to at least we can type check the following smoke test.

#![feature(in_place_initialization)]
use std::init::Init;

fn smoke() -> impl Init<[i32; 1]> {
    init # [0]
}

fn main() {}

Areas to improve

As of July 7th, I see that I need to work these out

  • The AST node InitTail is not necessary, I should just drop it
  • The HIR node InitKind::Array could be squashed so that it is just a list of InitTail nodes
  • The HIR node names. I don't find them descriptive. InitTail sounds terrible and I regret the choice.

r? @compiler-errors

This patch allows parsing of the syntax
`init [ v1, v2, v3, .. ]` and certain level of
type checking on HIR level.

Signed-off-by: Xiangfei Ding <dingxiangfei2009@protonmail.ch>
@rustbot rustbot added PG-exploit-mitigations Project group: Exploit mitigations T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Jul 7, 2025
@rust-log-analyzer
Copy link
Collaborator

The job tidy failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
[TIMING] core::build_steps::tool::Tidy { compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu, forced_compiler: false }, target: x86_64-unknown-linux-gnu } -- 0.000
fmt check
Diff in /checkout/compiler/rustc_parse/src/parser/stmt.rs:194:
 
     fn parse_stmt_path_start(&mut self, lo: Span, attrs: AttrWrapper) -> PResult<'a, Stmt> {
         let stmt = self.collect_tokens(None, attrs, ForceCollect::No, |this, attrs| {
-if this.check_init_block() {
+            if this.check_init_block() {
                 let expr = this.parse_init_block()?;
                 return Ok((
                     this.mk_stmt(rustc_span::DUMMY_SP, StmtKind::Expr(expr)),
fmt: checked 6150 files
Build completed unsuccessfully in 0:00:45
  local time: Mon Jul  7 00:08:54 UTC 2025
  network time: Mon, 07 Jul 2025 00:08:54 GMT
##[error]Process completed with exit code 1.

@bors
Copy link
Collaborator

bors commented Jul 7, 2025

☔ The latest upstream changes (presumably #143582) made this pull request unmergeable. Please resolve the merge conflicts.

@bors bors added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jul 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants